﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class DeleteInsulationMaterials : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			Transaction transaction = null;
			FormDeleteInsulationLayers formDeleteInsulationLayers = new FormDeleteInsulationLayers();
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Autodesk.Revit.DB.View activeView = document.ActiveView;
				Selection selection = activeUIDocument.Selection;
				if (DeleteInsulationMaterials.hWndRevit == null)
				{
					DeleteInsulationMaterials.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				formDeleteInsulationLayers.Show(DeleteInsulationMaterials.hWndRevit);
				int wMsg = 7;
				DeleteInsulationMaterials.SendMessage(DeleteInsulationMaterials.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				Element element = null;
				IList<Element> list = new List<Element>();
				for (;;)
				{
					try
					{
						if (formDeleteInsulationLayers.GetDeleteType())
						{
							Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new PipeOrDuctFilter(), "请选择需要删除绝热层的管道系统……");
							element = document.GetElementByRef(reference);
						}
						else
						{
							list = selection.PickElementsByRectangle(new YJKPipeOrDuctInsulationFilter(), "请框选需要删除的管道绝热层……");
						}
					}
					catch (Exception ex)
					{
						ex.Message.ToString();
						if (formDeleteInsulationLayers.BeManualCancel)
						{
							formDeleteInsulationLayers.BeManualCancel = false;
							continue;
						}
						break;
					}
					transaction = new Transaction(document, "删除绝热层");
					transaction.Start();
					if (formDeleteInsulationLayers.GetDeleteType() && element != null)
					{
						this.DeleteInsulationBySystemName(element);
					}
					else if (!formDeleteInsulationLayers.GetDeleteType() && list != null && list.Count > 0)
					{
						foreach (Element elem in list)
						{
							document.DeleteElement(elem);
						}
					}
					transaction.Commit();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				formDeleteInsulationLayers.Close();
			}
			return result;
		}

		private void DeleteInsulationBySystemName(Element systemElem)
		{
			Document document = systemElem.Document;
			List<Element> list = new List<Element>();
			if (systemElem is Pipe)
			{
				string systemName = (systemElem as Pipe).GetSystemName();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				filteredElementCollector.OfClass(typeof(PipeInsulation));
				IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
				list = new List<Element>();
				enumerator.Reset();
				while (enumerator.MoveNext())
				{
					Element elem = enumerator.Current;
					Parameter parameter = elem.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM);
					if (parameter != null && !(parameter.AsString() != systemName))
					{
						list.Add(enumerator.Current);
					}
				}
			}
			else if (systemElem is Duct)
			{
				string systemName = (systemElem as Duct).GetSystemName();
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
				filteredElementCollector2.OfClass(typeof(DuctInsulation));
				IEnumerator<Element> enumerator = filteredElementCollector2.GetEnumerator();
				list = new List<Element>();
				enumerator.Reset();
				while (enumerator.MoveNext())
				{
					Element elem2 = enumerator.Current;
					Parameter parameter2 = elem2.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM);
					if (parameter2 != null && !(parameter2.AsString() != systemName))
					{
						list.Add(enumerator.Current);
					}
				}
			}
			foreach (Element elem3 in list)
			{
				document.DeleteElement(elem3);
			}
		}

		public static YJKWindowHandle hWndRevit;
	}
}
